package com.eightywork.life.service;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.eightywork.life.model.Space;
import com.eightywork.life.util.HttpUtil;
import com.eightywork.life.util.MyHttpException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class CpService extends BaseService {
	private static final String URL = DOMAIN + "dapi/cp.php";

	/**
	 * 2.26 设置主题图片接口
	 * 
	 * @param touid
	 * @param plid
	 * @param message
	 * @throws MyHttpException
	 */
	public static void profiletheme(String picid) throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("picid", picid);
		params.put("m_auth", CacheService.getSpace().getmAuth());
		params.put("client", "android");
		try {
			String json = HttpUtil.post(URL + "?ac=profile&op=theme", params);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") != 0) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 2.12 发私信接口
	 * 
	 * @param touid
	 *            对方的uid
	 * @param plid
	 *            对话列表的id
	 * @param message
	 * @throws MyHttpException
	 */
	public static void pmsend(String touid, String message)
			throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("touid", touid);
//		params.put("plid", plid);
		params.put("message", message);
		params.put("pmsubmit", "1");
		params.put("client", "android");
		params.put("m_auth", CacheService.getSpace().getmAuth());
//		System.out.println("plid-in->" + plid);
//		System.out.println("touid-in->" + touid);
		try {
			String json = HttpUtil.post(URL+"?ac=pm&op=send", params);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") != 0) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发表圈子吐槽接口
	 * 
	 * @param tagid
	 *            圈子id
	 * @param title
	 *            吐槽标题
	 * @param message
	 *            吐槽内容
	 * @param forward_id
	 *            原吐槽id
	 * @throws MyHttpException
	 */
	public static String mtag(String tagid, String title, String message,
			String forward_id, String picids) throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("tagid", tagid);
		params.put("title", title);
		params.put("message", message);
		if (forward_id != null) {
			params.put("forward_id", forward_id);
			params.put("forward_idtype", "mtalkid");
		}
		params.put("picids", picids);
		params.put("addsubmit", "1");
		params.put("from", "android");
		params.put("client", "android");
		params.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL+"?ac=mtag", params);
			JSONObject jsonObject = new JSONObject(json);
			return jsonObject.optString("msg");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 个人认证信息保存接口
	 * 
	 * @param realname
	 *            实名
	 * @param idcard
	 *            身份证
	 * @param roomid
	 *            房间号
	 * @throws MyHttpException
	 */
	public static void profileVerify(String realname, String mobile,
			String roomid) throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("realname", realname);
		params.put("mobile", mobile);
		params.put("roomid", roomid);
		params.put("client", "android");
		params.put("profilesubmit", "1");
		params.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL + "?ac=profile&op=verify", params);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") != 0) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 1．22.01、未读私信数量接口
	 * @param
	 */
	 public static long pmcount() throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "pm");
		map.put("op", "newpm");
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.get(URL, map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				return jsonObject.optJSONObject("data").optInt("newpm");
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 个人联系方式设置保存接口
	 * 
	 * @param qq
	 * @param mobile
	 *            手机号
	 * @param friendqq
	 *            qq隐私 0:全用户可见 1:仅好友可见 3:仅自己可见
	 * @param friendmobile
	 * @throws MyHttpException
	 */
	public static void profileContact(String qq, String mobile,
			String friendqq, String friendmobile) throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("qq", qq);
		params.put("mobile", mobile);
		params.put("friend[qq]", friendqq);
		params.put("friend[mobile]", friendmobile);
		params.put("profilesubmit", "1");
		params.put("client", "android");
		params.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL+"?ac=comment", params);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") != 0) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param name
	 *            昵称
	 * @param birthyear
	 *            出生年
	 * @param birthmonth
	 *            出生月
	 * @param birthday
	 *            出生日
	 * @param marry
	 *            婚恋状态 1 未婚 2 已婚
	 * @param sex
	 *            性别 1 男 2 女
	 * @param birthprovince
	 *            出生省份
	 * @param birthcity
	 *            出生城市
	 * @param qq
	 * @param friendbirth
	 *            生日隐私 0:全用户可见 1:仅好友可见 3:仅自己可见
	 * @param friendmarry
	 *            婚恋状态隐私 0:全用户可见 1:仅好友可见 3:仅自己可见
	 * @param friendbirthcity
	 *            出生地隐私 0:全用户可见 1:仅好友可见 3:仅自己可见
	 * @param friendqq
	 *            qq隐私 0:全用户可见 1:仅好友可见 3:仅自己可见
	 * @throws MyHttpException
	 */
	public static void profileBase(String name, String birthyear,
			String birthmonth, String birthday, String marry, String sex,
			String birthprovince, String birthcity, String qq, String friendbirth,
			String friendmarry, String friendbirthcity, String friendqq,
			String friendsex, String friendmobile, String mobile,
			String note, String address) throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("name", name);
		params.put("birthyear", birthyear);
		params.put("birthmonth", birthmonth);
		params.put("birthday", birthday);
		params.put("marry", marry);
		params.put("sex", sex);
		params.put("birthprovince", birthprovince);
		params.put("birthcity", birthcity);
		params.put("qq", qq);
		params.put("friend[birth]", friendbirth);
		params.put("friend[marry]", friendmarry);
		params.put("friend[birthcity]", friendbirthcity);
		params.put("friend[qq]", friendqq);
		params.put("friend[sex]", friendsex);
		params.put("friend[mobile]", friendmobile);
		params.put("mobile", mobile);
		params.put("note", note);
		params.put("client", "android");
		params.put("verify_address", address);
		params.put("profilesubmit", "1");
		params.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL + "?ac=profile&op=base", params);
			Log.d("profileBase", json);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") != 0) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发表评论接口
	 * 
	 * @param id
	 * @param idtype
	 * @param message
	 * @throws MyHttpException
	 */
	public static void comment(String id, String idtype, String message)
			throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("id", id);
		params.put("idtype", idtype);
		params.put("message", message);
		params.put("client", "android");
		params.put("commentsubmit", "1");
		params.put("m_auth", CacheService.getSpace().getmAuth());
	//	System.out.println("------>---" + params.toString());
		try {
			String json = HttpUtil.post(URL + "?ac=comment", params);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") != 0) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 申请加入圈子接口
	 * 
	 * @param tagid
	 *            圈子id, 批量加入用 | 连接, 如：1|2|3
	 * @throws MyHttpException
	 */
	public static JSONObject mtagjoin(String tagid) throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("tagid", tagid);
		params.put("joinsubmit", "1");
		params.put("client", "android");
		params.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL+"?ac=mtag&op=join", params);
			return new JSONObject(json);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 退出圈子接口
	 * 
	 * @param tagid
	 *            圈子id, 批量加入用 | 连接, 如：1|2|3
	 * @throws MyHttpException
	 */
	public static JSONObject mtagout(String tagid) throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("tagid", tagid);
		params.put("outsubmit", "1");
		params.put("client", "android");
		params.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL+"?ac=mtag&op=out", params);
			return new JSONObject(json);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 上传照片接口
	 * 
	 * @return
	 * @throws MyHttpException
	 */
	public static long uploadphoto(File file) throws MyHttpException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("ac", "upload");
		params.put("op", "uploadphoto");
		params.put("client", "android");
		params.put("m_auth", CacheService.getSpace().getmAuth());
		Map<String, File> files = new HashMap<String, File>();
		files.put("Filedata", file);
		try {
			String json = HttpUtil.post(URL, params, files);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				return jsonObject.optJSONObject("data").optLong("picid");
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 * 发表/转发吐槽接口
	 * 
	 * @param title
	 *            吐槽标题(可选)
	 * @param message
	 *            吐槽内容
	 * @param classid
	 *            吐槽分类
	 * @param longitude
	 *            经度
	 * @param latitude
	 *            维度
	 * @param location
	 *            发布地址
	 * @param picids
	 *            图片ids, 多个用|连接, 调用 <2.14 上传照片接口>
	 * @param forward_id
	 *            原吐槽id
	 * @return
	 * @throws MyHttpException
	 */
	public static String addtalk(String title, String message, String classid,
			String longitude, String latitude, String location, String picids,
			String forward_id, String forward_type) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		if (!title.equals(""))
			map.put("title", title);
		map.put("message", message);
		if (!classid.equals(""))
			map.put("classid", classid);
		map.put("longitude", longitude);
		map.put("latitude", latitude);
		map.put("location", location);
		map.put("picids", picids);
		if (forward_id != null) {
			map.put("forward_id", forward_id);
			map.put("forward_idtype", forward_type);
		}
		map.put("from", "android");
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
	//	System.out.println("====发表/转发吐槽接口=====>" + map.toString());
		try {
			String json = HttpUtil.post(URL+"?ac=talk&op=addtalk", map);
		//	System.out.println("====json=====>" + json);
			JSONObject jsonObject = new JSONObject(json);
	//		System.out.println("====error=====>" + jsonObject.optString("error"));
	//		System.out.println("====msg=====>" + jsonObject.optString("msg"));
			if (jsonObject.getInt("error") == 0) {
				return jsonObject.optString("msg");
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 个人设置读取接口
	 * 
	 * @return
	 * @throws MyHttpException
	 */
	public static Space profile() throws MyHttpException {
		long start = System.currentTimeMillis();
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "profile");
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		map.put("time", System.currentTimeMillis() + "");
		Space object = null;
		try {
			String json = HttpUtil.get(URL, map);
			Log.d("Space", json);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				String data = jsonObject.optString("data");
				Gson gson = new GsonBuilder().serializeNulls().create();
				object = gson.fromJson(data, Space.class);
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		Log.d("profile", System.currentTimeMillis() - start + "");
		return object;
	}

	/**
	 * 好友添加申请列表接口
	 * 
	 * @return
	 * @throws MyHttpException
	 */
	public static List<Space> friendRequest() throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "friend");
		map.put("op", "request");
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		List<Space> list = null;
		try {
			String json = HttpUtil.get(URL, map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				String data = jsonObject.optString("data");
				Gson gson = new GsonBuilder().serializeNulls().create();
				list = gson.fromJson(data, new TypeToken<List<Space>>() {
				}.getType());
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 修改头像接口
	 * 
	 * @param Filedata
	 *            文件上传变量
	 * @return
	 * @throws MyHttpException
	 */
	public static int avatar(File Filedata) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "avatar");
		map.put("avatarsubmit", "1");
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		Map<String, File> files = new HashMap<String, File>();
		files.put("Filedata", Filedata);
		try {
			String json = HttpUtil.post(URL, map, files);
			Log.d("avatar", json);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				return jsonObject.optJSONObject("data").optJSONObject("reward")
						.optInt("credit");
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 修改昵称接口
	 * 
	 * @param name
	 * @return
	 * @throws MyHttpException
	 */
	public static int name(String name) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("namesubmit", "1");
		map.put("name", name);
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL+"?ac=name", map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				return jsonObject.optJSONObject("data").optJSONObject("reward")
						.optInt("credit");
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 2.10 申请成为好友接口
	 * 
	 * @param applyuid
	 *            对方的uid；批量申请时，多个uid用|连接，如：1|2| 3
	 * @param gid
	 * @param note
	 * @throws MyHttpException
	 */
	public static String friendAdd(String gid, String uid, String note)
			throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("addsubmit", "1");
		map.put("applyuid", uid);
		Log.d("uid", uid);
		map.put("gid", gid);
		map.put("note", note);
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL + "?ac=friend&op=add", map);
			JSONObject jsonObject = new JSONObject(json);
			return jsonObject.optString("msg");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 取消好友关注接口
	 * 
	 * @param applyuid
	 * @throws MyHttpException
	 */
	public static String friendIgnore(String applyuid) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("applyuid", applyuid);
		map.put("friendsubmit", "1");
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL+"?ac=friend&op=ignore", map);
			JSONObject jsonObject = new JSONObject(json);
			return jsonObject.optString("msg");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 批量申请成为好友接口
	 * 
	 * @param applyuid
	 * @param gid
	 * @param note
	 * @throws MyHttpException
	 */
	public static void friendMulAdd(String applyuid,  String note)
			throws MyHttpException {
		Log.d("applyuid", applyuid);
		Map<String, String> map = new HashMap<String, String>();
		map.put("addsubmit", "1");
		map.put("applyuid", applyuid);
		map.put("note", note);
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL+"?ac=friendmul&op=add", map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.optBoolean("error")) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 同意成为好友接口
	 * 
	 * @param applyuid
	 *            申请人的uid
	 * @param gid
	 * @throws MyHttpException
	 */
	public static void friendAgree(String applyuid, String gid)
			throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("agreesubmit", "1");
		map.put("applyuid", applyuid);
		map.put("gid", gid);
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL+"?ac=friend&op=add", map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.optBoolean("error")) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改密码接口
	 * 
	 * @param password
	 * @param newpasswd1
	 * @param newpasswd2
	 * @throws MyHttpException
	 */
	public static void account(String password, String newpasswd1,
			String newpasswd2) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("pwdsubmit", "1");
		map.put("password", password);
		map.put("newpasswd1", newpasswd1);
		map.put("newpasswd2", newpasswd2);
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		try {
			String json = HttpUtil.post(URL+"?ac=account", map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.optBoolean("error")) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
